<template>
  <el-card style="margin: 300px; width: 600px;">
 <h1>用户登录</h1>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="账号" prop="Name">
      <el-input v-model="ruleForm.Name" />
    </el-form-item>
    <el-form-item label="密码" prop="Pwd">
      <el-input v-model="ruleForm.Pwd" show-password/>
    </el-form-item>
    
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
  </el-card>
 
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, toValue } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios'
import { useRouter } from 'vue-router'
import read from '../../http/AxiosHelper';
const router=useRouter();
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  Name: '',
  Pwd: '',
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive({
  Name: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
   
  ],
  Pwd: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
   
  ],

})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      read.get("/api/Login/UserLogin",{params:ruleForm}).then(res=>{
        if(res.data.code==200){
          ElMessage.success(res.data.msg);
          localStorage.setItem("id",res.data.data.id);
          localStorage.setItem("name",ruleForm.Name);
          localStorage.setItem("rolename",res.data.data.roleName);
          localStorage.setItem("bid",res.data.data.bid);
          localStorage.setItem("rid",res.data.data.rid);
          localStorage.setItem("token",res.data.data.jwtToken);
          router.push('/menu');
        }
        else{
          ElMessage.error(res.data.msg);
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}


const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>


